//
//  VMCCustomPickerView.m
//  vmcshop
//
//  Created by toggle on 15/6/30.
//  Copyright (c) 2015年 idongler. All rights reserved.
//

#import "VMCCustomPickerView.h"

@interface VMCCustomPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic, strong)UIPickerView *picker;
@property (nonatomic, strong)UIButton *sureBtn;
@property (nonatomic, strong)UIButton *cancelBtn;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UIView *view;
@property (nonatomic, strong)UIView *headView;
@property (nonatomic, copy)		NSString *title; //标题
@property (nonatomic, strong)   NSArray *data;   //数据源

@end

@implementation VMCCustomPickerView

+(instancetype)showPickerAddToView:(UIView *)view title:(NSString *)title withDataArray:(NSArray *)data withBlock:(myBlock)block
{
	VMCCustomPickerView *pickerView = [[self alloc] initWithView:view title:title withDataArray:data withBlock:(myBlock)block];
	[view addSubview:pickerView];
	return pickerView;
}
+(BOOL)hidePickerForView:(UIView *)view
{
	VMCCustomPickerView *pickerView = [self pickerForView:view];
	if (pickerView != nil)
	{
		[pickerView removeFromSuperview];
		return YES;
	}
	return NO;
}

- (id)initWithView:(UIView *)view title:(NSString *)title withDataArray:(NSArray *)data withBlock:(myBlock)block
{
	NSAssert(view, @"View must not be nil.");
	_data = [data copy];
	_title = [title copy];
	_block = block;
	VMCCustomPickerView *pickerView = [self initWithFrame:view.bounds];
	pickerView.backgroundColor = RGBA(0, 0, 0, 0.5);
	_view = view;
	_picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-216, CGRectGetWidth(self.frame), 216)];
	_picker.delegate = self;
	_picker.dataSource = self;
	_picker.backgroundColor = [UIColor whiteColor];
	[pickerView addSubview:_picker];

	UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidePickerView:)];
	[self addGestureRecognizer:tap];

	[self initHeadView];

	return pickerView;
}
- (void)hidePickerView:(UITapGestureRecognizer *)tap
{
	[self removeSubView];
}
- (void)initHeadView
{
	_headView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(_picker.frame)-44, CGRectGetWidth(self.frame), 44)];
	_headView.backgroundColor = RGB(241, 241, 241);
	[self addSubview:_headView];

	_cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	_cancelBtn.frame = CGRectMake(10, CGRectGetHeight(_headView.frame)/2-30/2, 50, 30);
	[_cancelBtn addTarget:self action:@selector(sureBtn:) forControlEvents:UIControlEventTouchUpInside];
	[_cancelBtn setAttributedTitle:ATTRIBUTE_STRING(@"取消", 12.0f,RGB(78, 129, 41)) forState:UIControlStateNormal];
	[_headView addSubview:_cancelBtn];

	_sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
	_sureBtn.frame = CGRectMake(CGRectGetWidth(_headView.frame)-50-10, CGRectGetHeight(_headView.frame)/2-30/2, 50, 30);
	[_sureBtn addTarget:self action:@selector(sureBtn:) forControlEvents:UIControlEventTouchUpInside];
	[_sureBtn setAttributedTitle:ATTRIBUTE_STRING(@"确定", 12.0f,RGB(78, 129, 41)) forState:UIControlStateNormal];
	[_headView addSubview:_sureBtn];

	_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_cancelBtn.frame)+5, CGRectGetHeight(_headView.frame)/2-30/2, CGRectGetMinX(_sureBtn.frame)-CGRectGetMaxX(_cancelBtn.frame)+5, 30)];
	_titleLabel.font = [UIFont systemFontOfSize:10.0];
	_titleLabel.textColor = [UIColor blackColor];
	_titleLabel.text = _title;
	_titleLabel.textAlignment = NSTextAlignmentCenter;
	[_headView addSubview:_titleLabel];

	if ([_title isEqualToString:@"请选择服药时间"])
	{
		[_picker selectRow:8 inComponent:0 animated:NO];
		[_picker selectRow:0 inComponent:1 animated:NO];
	}

}
- (void)sureBtn:(UIButton *)btn
{
	NSMutableArray *dataArray = [NSMutableArray array];
	if(btn == _cancelBtn)
	{

	}
	else
	{
		if (_data.count > 0)
		{
			for (int i = 0; i<_data.count; i++)
			{
				NSInteger num = [_picker selectedRowInComponent:i];
				NSArray *arry = _data[i];
				NSString *string = arry[num];
				[dataArray addObject:string];
			}
		}
		else
		{

		}
		if (dataArray.count > 0)
		{
			_block(dataArray);
		}
		else
		{
			return;
		}
	}
	[self removeSubView];
}

- (void)removeSubView
{
	NSEnumerator *subviewsEnum = [_view.subviews reverseObjectEnumerator];
	for (UIView *subview in subviewsEnum) {
		if ([subview isKindOfClass:[VMCCustomPickerView class]])
		{
			[UIView animateWithDuration:0.3 animations:^{

			} completion:^(BOOL finished) {
				[subview removeFromSuperview];
			}];

		}
	}
}

+ (instancetype)pickerForView:(UIView *)view
{
	NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
	for (UIView *subview in subviewsEnum) {
		if ([subview isKindOfClass:self])
		{
			return (VMCCustomPickerView *)subview;
		}
	}
	return nil;
}
#pragma Mark UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return _data.count;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	if (_data.count > 0)
	{
		NSArray *array = _data[component];
		return array.count;
	}
	else
	{
		return 0;
	}
}
//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	if (_data.count > 0)
	{
		NSArray *arr = _data[component];

		return arr[row];
	}
	else
	{
		return 0;
	}

}
//- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0); // attributed title is favored if both methods are implemented
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
